module AboutYou
  module SDK
    module Model
      ###
      # This class represents a category model
      ###
      class Category
        # all categories
        ALL = false
        # only active categories
        ACTIVE_ONLY = true

        # id of the category
        attr_accessor :id
        # name of the category
        attr_accessor :name
        # boolean determining whether the category is active
        attr_accessor :is_active
        # position of the category
        attr_accessor :position
        # id of the parent category
        attr_accessor :parent_id
        # Array containing all subcategories
        attr_accessor :all_subcategories
        # Array containing all active subcategories
        attr_accessor :active_subcategories
        # count of products in category
        attr_accessor :product_count
        # instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
        attr_accessor :category_manager

        ###
        # Constructor for the AboutYou::SDK::Model::Category class
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::Category
        ###
        def initialize
          self.all_subcategories = []
          self.active_subcategories = []

          self
        end

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        #   - +cat_manager+ -> instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::Category
        ###
        def self.create_from_json(json_object, cat_manager)
          category = new

          category.parent_id        = json_object['parent']
          category.id               = json_object['id']
          category.name             = json_object['name']
          category.is_active        = json_object['active']
          category.position         = json_object['position']
          category.category_manager = cat_manager

          category
        end

        ###
        # This method checks if the complete category path up to this category is
        # active
        #
        # * *Returns* :
        #   - Boolean determining whether the complete path up to this category is active or not
        ###
        def path_active?
          is_active && (parent.nil? || parent.path_active?)
        end

        ###
        # This method returns the parent category
        #
        # * *Returns* :
        #   - Either instance of AboutYou::SDK::Model::Category or nil if no parent category is set
        ###
        def parent
          return nil unless parent_id

          category_manager.category(parent_id)
        end

        ###
        # This method is used for getting all subcategories
        #
        # * *Args*    :
        #   - +active_only+ -> boolean controlling whether the result should only contain active categories or not
        #
        # * *Returns* :
        #   - Hash containing pairs of category_id => instance of AboutYou::SDK::Model::Category
        ###
        def subcategories(active_only = ACTIVE_ONLY)
          subcategories = category_manager.subcategories(id, active_only)
          return subcategories if active_only == ALL

          result = {}

          subcategories.each do |key, subcat|
            result[key] = subcat if subcat.is_active
          end

          result
        end

        ###
        # This method is used for getting the breadcrumb up to this category
        #
        # * *Returns* :
        #   - Array containing instances of AboutYou::SDK::Model::Category
        ###
        def breadcrumb
          breadcrumb = parent ? parent.breadcrumb : []
          breadcrumb.push(self)

          breadcrumb
        end
      end
    end
  end
end
